import Vue from 'vue'
import VueRouter from 'vue-router'
import Movie from '@/views/movie/chilMovie/Movie'
import nowPlaying from '@/views/movie/chilMovie/NowPlaying'
import comingSoon from '@/views/movie/chilMovie/ComingSoon'
import Index from '@/views/movie/index'
import Film from '@/views/movie/film'
import MovieCinemas from '@/views/movie/cinemas'
import My from '@/views/my/My'
import Cinema from '@/views/cinemas/Cinemas'
import CinemasFilm from '@/views/cinemas/film'
import News from '@/views/news/News'
import store from '@/store/index'
import City from '@/views/city/city'
import Login from '@/views/my/login'
import Register from '@/views/my/register'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/movie',
    component: Index,
    children: [
      {
        path: 'movie',
        component: Movie,
        name: 'movie'
      },
      {
        path: 'nowPlaying',
        component: nowPlaying,
      },
      {
        path: 'comingSoon',
        component: comingSoon,
      }
    ]
  },
  {
    path: '/movie/cinemas',
    component: MovieCinemas,
  },
  {
    path: '/film',
    component: Film
  },
  {
    path: '/my',
    component: My
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/cinema',
    component: Cinema
  },
  {
    path: '/cinema/film',
    component: CinemasFilm
  },
  {
    path: '/news',
    component: News
  },
  {
    path: '/city',
    component: City
  }

]

const router = new VueRouter({
  routes,
  mode: 'hash'
})
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

router.beforeEach((to, from, next) => {
  let arr = ['/movie', '/cinema', '/news', '/my']
  let index = arr.indexOf(to.path)
  store.commit('changeActive', index)
  next()
})
export default router
